package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.Team;
import com.ruoyi.common.utils.NumberUtils;
import com.ruoyi.system.service.ISysTeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @author : hcl
 * @Date : 2023/8/3
 */
@RestController
@RequestMapping("/home/team")
public class SysHomeController extends BaseController {
    @Autowired
    private ISysTeamService teamService;

    @GetMapping("/list")
    public AjaxResult teamList() {
        return success(teamService.selectTeamByUserId(getUserId()));
    }

    @PostMapping("/add")
    public AjaxResult addTeam(@Valid Team team) {
        teamService.createTeam(team, getUserId());
        return success();
    }

    @PostMapping("/change")
    public AjaxResult addTeam(HttpServletRequest request, Long teamId) {
        if (NumberUtils.isTrue(teamId)) {
            List<Team> teams = teamService.selectTeamByUserId(getUserId());
            Team team = teams.stream().filter(t -> t.getId().equals(teamId)).findFirst().orElse(null);
            if (team == null) {
                return error("项目不存在");
            }
            return teamService.meta(request,team);
        }
        return error("请选择项目");
    }
}
